<?php


namespace app\common\listener;
use app\admin\model\base\Practise;
use app\admin\model\base\StudentPractiseSubject;
use app\admin\model\base\StudentRanking;
use app\admin\model\StudentPractise;

class PaperListener
{
    public function handle($event)
    {
        //计算用户得分
        $info = StudentPractise::where('id','=',$event['id'])->with(['practise'])->find();
        $cate_id = $info->practise_id;
        //查询练习ID
        $num = StudentPractise::where('student_id','=',$info->student_id)
            ->where('status','=',1)
            ->where('practise_id','=',$cate_id)
            ->count();
        //处理所有题目
        $score = 0;
        if($info){
            $list = StudentPractiseSubject::where('student_practise_id','=',$event['id'])->select()->toArray();
            if(!empty($list)){
                foreach ($list as $item){
                    if($item['student_analysis'] != ''){
                        if($item['type'] == 1){
                            if($item['analysis'] == $item['student_analysis']){
                                //正确加分
                                $score = $score + $item['weighting'];
                                //更新学生分值
                                StudentPractiseSubject::where('id','=',$item['id'])->update(['score'=>$item['weighting']]);
                            }
                        }elseif ($item['type'] == 1){
                            if(!empty(array_diff(array_values(array_filter(explode(',',$item['analysis']))),array_values(array_filter(explode(',',$item['student_analysis'])))))){
                                //正确加分
                                $score = $score + $item['weighting'];
                                //更新学生分值
                                StudentPractiseSubject::where('id','=',$item['id'])->update(['score'=>$item['weighting']]);
                            }
                        }
                    }
                }
            }
        }
        if($score > 0){
            //更新
            $info->score = $score;
            $info->save();
        }
        //更新排行版
        $infoRanking2 = StudentRanking::where('student_id','=',$info->student_id)->where('type','=',2)
            ->where('practise_id','=',$cate_id)->find();
        if($infoRanking2){
            //更新
            $infoRanking2->score = $num;
            $infoRanking2->save();
        }else{
            //创建
            StudentRanking::create([
                'student_id'=>$info->student_id,
                'practise_id'=>$cate_id,
                'score'=>$num,
                'type'=>2,
            ]);
        }

        $infoRanking = StudentRanking::where('student_id','=',$info->student_id)->where('type','=',1)
            ->where('practise_id','=',$cate_id)->find();
        if($infoRanking){
            //更新
            $infoRanking->score = $score;
            $infoRanking->save();
        }else{
            //创建
            StudentRanking::create([
                'student_id'=>$info->student_id,
                'practise_id'=>$cate_id,
                'score'=>$score,
                'type'=>1,
            ]);
        }

    }
}